package com.tanhua.dubbo.api.db;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.tanhua.dubbo.api.mapper.UserInfoMapper;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户信息服务接口实现类
 */
@DubboService
public class UserInfoApiImpl implements UserInfoApi{

    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     * 保存用户信息
     * @param userInfo
     */
    @Override
    public void add(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    /**
     *更新tb_user_info表 数据
     * @param userInfo
     */
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 根据用户id查询用户信息
     * @param currentUserId
     * @return
     */
    @Override
    public UserInfo findUserInfo(Long currentUserId) {
        return userInfoMapper.selectById(currentUserId);
    }

    /**
     * 根据黑名单用户ids查询黑名单用户信息
     *
     * @param ids
     * @return
     */
    @Override
    public List<UserInfo> findUsersByBatchIds(List<Long> ids) {
        if (!CollectionUtils.isEmpty(ids)){
            return userInfoMapper.selectBatchIds(ids);
        }
        return null;
    }

    /**
     * 根据userIds 查询条件得到用户信息
     *
     * @param listUserIds
     * @param userInfoCondition
     * @return
     */
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> listUserIds, UserInfo userInfoCondition) {
        //1 构造查询条件
        if (CollectionUtil.isNotEmpty(listUserIds)) {
            LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
            List<UserInfo> userInfoList = new ArrayList<>();
            if (userInfoCondition != null) {
                Integer age = userInfoCondition.getAge();
                String gender = userInfoCondition.getGender();
                String nickname = userInfoCondition.getNickname();
                if (StringUtils.isNotBlank(nickname)) {
                    queryWrapper.like(UserInfo::getNickname, nickname);
                }
                if (StringUtils.isNotBlank(gender)) {
                    queryWrapper.eq(UserInfo::getGender, gender);
                }
                if (age != null) {
                    queryWrapper.lt(UserInfo::getAge, age);
                }
                queryWrapper.in(UserInfo::getId, listUserIds);
                userInfoList = userInfoMapper.selectList(queryWrapper);
            } else {

                //2 根据条件查询tb_user_info List<UserInfo>
                userInfoList = userInfoMapper.selectBatchIds(listUserIds);
            }

            //3 将List<UserInfo>转为Map<Long,UserInfo>
            Map<Long, UserInfo> userInfoMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(userInfoList)) {
            /*for (UserInfo userInfo : userInfoList) {
                userInfoMap.put(userInfo.getId(),userInfo);
            }*/
                userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, b -> b));
            }
            //4 返回结果
            return userInfoMap;
        }
        return null;
    }

    /**
     * 返回该性别的用户
     *
     * @param gender
     * @return
     */
    @Override
    public List<UserInfo> findOtherSex(String gender) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserInfo::getGender,gender);
        lqw.lt(UserInfo::getId,80);
        return userInfoMapper.selectList(lqw);
    }


}
